package com.zzw.basic.io;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

public class CharsetTransform {

	public static void main(String[] args) {
		try {
			Charset cn = Charset.forName("GBK");
			CharsetEncoder charsetEncoder = cn.newEncoder();
			CharsetDecoder charsetDecoder = cn.newDecoder();
			CharBuffer buffer = CharBuffer.allocate(8);
			buffer.put("a");
			buffer.put("b");
			buffer.flip();
			ByteBuffer byteBuffer = charsetEncoder.encode(buffer);
			for(int i = 0;i < byteBuffer.capacity();i++){
				System.out.println(byteBuffer.get(i) + " ");
			}
			System.out.println("\n" + charsetDecoder.decode(byteBuffer));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
